package com.nemlistapp;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ShoppingListFetcher extends AsyncTask<Integer, Void, ArrayList<Product>> {
	
	private String NAMESPACE;
	private String METHOD_NAME = "GetProductsWithListId";
	private String SOAP_ACTION = "http://tempuri.org/IListService/GetProductsWithListId";
	
	private String URL;
	private SoapSerializationEnvelope envelope;
	private ProgressDialog progressDialog;
	private Context context;
	
	public ShoppingListFetcher(Context context)
	{
		this.context = context;
	}

	@Override
	protected ArrayList<Product> doInBackground(Integer... params) {
		int shoppingListId = params[0];
		
		String userName = States.getInstance().getUserName();
		String token = States.getInstance().getToken();
		
		NAMESPACE = context.getString(R.string.ws_namespace);
		URL = context.getString(R.string.ws_url);
		
		SoapObject request  = new SoapObject(NAMESPACE,METHOD_NAME);
		
		PropertyInfo shoppingListIdProperty = new PropertyInfo();
		PropertyInfo userNameProperty = new PropertyInfo();
		PropertyInfo tokenProperty = new PropertyInfo();
		
		shoppingListIdProperty.setName("listId");
		shoppingListIdProperty.setValue(shoppingListId);
		shoppingListIdProperty.setType(String.class);
		
		userNameProperty.setName("username");
		userNameProperty.setValue(userName);
		userNameProperty.setType(String.class);
		
		request.addProperty(shoppingListIdProperty);
		
		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		String result = null;
		HttpTransportSE httpRequest = new HttpTransportSE(URL);
		ArrayList<Product> products = new ArrayList<Product>();
		
	try{
			
			httpRequest.call(SOAP_ACTION, envelope);
			SoapObject response = (SoapObject)envelope.getResponse();
			for(int i = 0; i < response.getPropertyCount(); i++)
			{
				SoapObject soapObject = (SoapObject)response.getProperty(i);
				
				int amount = Integer.parseInt(soapObject.getPropertyAsString(0));
				String boughtString = soapObject.getProperty(1).toString();
				int productId = Integer.parseInt(soapObject.getPropertyAsString(2));
				String productName = soapObject.getPropertyAsString(3);
				boolean bought;
				if(boughtString.equalsIgnoreCase("true"))
				{
				bought = true;
				}
				else
				{
					bought = false;
				}
				
				products.add(new Product(productId, productName, amount, bought));
			}	
			
		} catch (Exception e){
			e.printStackTrace();
			
		}
		
		
		return products;
	}
	
	protected void onPostExecute(ArrayList<Product> result)
	{
		ShoppingListActivity activity = (ShoppingListActivity)context;
		activity.setProducts(result);
	}
	
}
	

